<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="活动名称" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
          <el-form-item label="活动时间" prop="time">
            <div class="block">
              <el-date-picker
                v-model="time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="timestamp"
              >
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item label="一级分类" prop="first_cateid">
            <el-select
              v-model="ruleForm.first_cateid"
              placeholder="请选择"
              @change="firstChange"
            >
              <el-option
                v-for="item in firstList"
                :key="item.id"
                :label="item.catename"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="二级分类" prop="second_cateid">
            <el-select v-model="ruleForm.second_cateid" placeholder="请选择" @change="secondChange">
              <el-option
                v-for="item in secondList"
                :key="item.id"
                :label="item.catename"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择商品" prop="goodsid">
            <el-select v-model="ruleForm.goodsid" placeholder="请选择">
              <el-option
                v-for="item in goodsList"
                :key="item.id"
                :label="item.goodsname"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="menus">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { categoryList, categoryInfo,goodsList,seckillAdd,seckillEdit } from "@/api.js";
export default {
  props: ["info"],
  data() {
    return {
      //一级分类下拉菜单
      firstList: [],
      //二级分类
      secondList: [],
      //商品
      goodsList:[],
      time: [],
      ruleForm: {
        title: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        begintime: "",
        endtime: "",
        status: 1,
      },
      rules: {
        title: [{ required: true, message: "活动名称", trigger: "blur" }],
        first_cateid: [
          { required: true, message: "请选择一级分类", trigger: "change" },
        ],
        second_cateid: [
          { required: true, message: "请选择二级分类", trigger: "change" },
        ],
        goodsid: [
          { required: true, message: "请选择三级分类", trigger: "change" },
        ],
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  async created() {
    //获取一级分类下拉数据
    let res1 = await categoryList();
    this.firstList = res1.list;
  },
  methods: {
    //一级分类改变需要获取对应的二级数据，每次都要获取
    async firstChange() {
      let res = await categoryInfo(this.ruleForm.first_cateid);
      this.secondList = res.list;
    },
    //二级分类改变需要获取对应的三级数据，每次都要获取
    async secondChange() {
      let res = await goodsList({first_cateid:this.ruleForm.first_cateid,second_cateid:this.ruleForm.second_cateid});
      this.goodsList = res.list;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.ruleForm.begintime=this.time[0]
          this.ruleForm.endtime=this.time[1]
          if (this.info.type === "add") {
            let res = await seckillAdd(this.ruleForm);
          } else {
            let res = await seckillEdit(this.ruleForm);
          }
          this.info.isShow = false;
          this.$emit("update");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //给表单复制
    setValue(info) {
      this.time=[]
      this.ruleForm = { ...info };
      if(this.info.type=="edit"){
        this.time.push(Number(this.ruleForm.begintime))
        this.time.push(Number(this.ruleForm.endtime))
        this.firstChange()
        this.secondChange()
      }
    },
  },
};
</script>
<style scoped>
</style>